package psrc.bd.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import psrc.logic.util.beans.FabricaBean;
import psrc.logic.util.beans.PagamentoComissao;

public class FabricaDAO extends AbstractDAO {

	public FabricaDAO(Connection conn) {
		super(conn);
	}

	@Override
	public <T> void adiciona(T _bean) throws SQLException{
		FabricaBean bean = (FabricaBean) _bean;
		String sql = "insert into Fabrica "
				+ "(razao_social, nome_fantasia, logradouro, numero, complemento, bairro, cidade, estado, cep, cnpj, inscricao_estadual, telefone, email, pagamento_comissao, comissao_direto, comissao_preposto) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CAST(? AS pagamento_comissao), ?, ?)";
		try {
			PreparedStatement stmt = connection.prepareStatement(sql);

			stmt.setString(1, bean.getRazaoSocial());
			stmt.setString(2, bean.getNomeFantasia());
			stmt.setString(3, bean.getLogradouro());
			stmt.setString(4, bean.getNumero());
			stmt.setString(5, bean.getComplemento());
			stmt.setString(6, bean.getBairro());
			stmt.setString(7, bean.getCidade());
			stmt.setString(8, bean.getEstado());
			stmt.setString(9, bean.getCep());
			stmt.setString(10, bean.getCnpj());
			stmt.setString(11, bean.getInscricaoEstadual());
			stmt.setString(12, bean.getTelefone());
			stmt.setString(13, bean.getEmail());
			//TODO toda fabrica criada esta hardcoded pagando a comissao via faturamento
			stmt.setString(14, bean.getPagamentoComissao() != null ? bean.getPagamentoComissao().toString() :  PagamentoComissao.faturamento.toString());
			stmt.setDouble(15, bean.getComissaoDireto() != null ? Double.valueOf(bean.getComissaoDireto()) : null);
			stmt.setDouble(16, bean.getComissaoPreposto() != null ? Double.valueOf(bean.getComissaoPreposto()) : 0);
			
			stmt.execute();
			stmt.close();

		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
	}

	@Override
	public <T> void remove(T _bean) throws SQLException{
		FabricaBean bean = (FabricaBean) _bean;
		try {
			PreparedStatement stmt = connection.prepareStatement("delete from Fabrica where id_fabrica=?");

			stmt.setInt(1, Integer.parseInt(bean.getIdFabrica()));

			stmt.execute();
			stmt.close();

		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
	}

	@Override
	public <T> void edita(T _bean) throws SQLException{
		FabricaBean bean = (FabricaBean) _bean;

		String sql = "update Fabrica set " +"razao_social=?, nome_fantasia=?, logradouro=?, numero=?, complemento=?, bairro=?, cidade=?, estado=?, cep=?, cnpj=?, inscricao_estadual=?, telefone=?, email=?, pagamento_comissao=?, comissao_direto=?, comissao_preposto=? where id_fabrica=?";
		try {
			PreparedStatement stmt = connection.prepareStatement(sql);

			stmt.setString(1, bean.getRazaoSocial());
			stmt.setString(2, bean.getNomeFantasia());
			stmt.setString(3, bean.getLogradouro());
			stmt.setString(4, bean.getNumero());
			stmt.setString(5, bean.getComplemento());
			stmt.setString(6, bean.getBairro());
			stmt.setString(7, bean.getCidade());
			stmt.setString(8, bean.getEstado());
			stmt.setString(9, bean.getCep());
			stmt.setString(10, bean.getCnpj());
			stmt.setString(11, bean.getInscricaoEstadual());
			stmt.setString(12, bean.getTelefone());
			stmt.setString(13, bean.getEmail());
			stmt.setString(14, bean.getPagamentoComissao() != null ? bean.getPagamentoComissao().toString() : (String) null);
			stmt.setDouble(15, Double.valueOf(bean.getComissaoDireto()));
			stmt.setDouble(16, Double.valueOf(bean.getComissaoPreposto()));
			stmt.setInt(17, Integer.parseInt(bean.getIdFabrica()));
			stmt.execute();
			stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
	}

	@Override
	public <T> List<T> seleciona() throws SQLException{
		List<FabricaBean> fabricas = new ArrayList<FabricaBean>();

		try {
			PreparedStatement stmt = connection.prepareStatement("select * from Fabrica");

			ResultSet rs = stmt.executeQuery();

			while (rs.next()) {
				FabricaBean bean = new FabricaBean();
				bean.setRazaoSocial(rs.getString("razao_social"));
				bean.setNomeFantasia(rs.getString("nome_fantasia"));
				bean.setLogradouro(rs.getString("logradouro"));
				bean.setNumero(rs.getString("numero"));
				bean.setComplemento(rs.getString("complemento"));
				bean.setBairro(rs.getString("bairro"));
				bean.setCidade(rs.getString("cidade"));
				bean.setEstado(rs.getString("estado"));
				bean.setCep(rs.getString("cep"));
				bean.setCnpj(rs.getString("cnpj"));
				bean.setInscricaoEstadual(rs.getString("inscricao_estadual"));
				bean.setTelefone(rs.getString("telefone"));
				bean.setEmail(rs.getString("email"));
				bean.setPagamentoComissao(PagamentoComissao.valueOf(rs.getString("pagamento_comissao")));
				bean.setComissaoDireto(rs.getString("comissao_direto"));
				bean.setComissaoPreposto(rs.getString("comissao_preposto"));
				bean.setIdFabrica(rs.getString("id_fabrica"));

				fabricas.add(bean);
			}
			rs.close();
			stmt.close();
			return (List<T>) fabricas;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
	}

@Override
	public <T> List<T> selecionaPorId(T param) throws SQLException {
		List<FabricaBean> fabricas = new ArrayList<FabricaBean>();
		FabricaBean user = (FabricaBean) param;
		try {
			PreparedStatement stmt = connection
					.prepareStatement("select * from Fabrica where id_fabrica=?");
			stmt.setString(1, user.getIdFabrica());
			ResultSet rs = stmt.executeQuery();
			
			while (rs.next()) {
				FabricaBean bean = new FabricaBean();
				bean.setRazaoSocial(rs.getString("razao_social"));
				bean.setNomeFantasia(rs.getString("nome_fantasia"));
				bean.setLogradouro(rs.getString("logradouro"));
				bean.setNumero(rs.getString("numero"));
				bean.setComplemento(rs.getString("complemento"));
				bean.setBairro(rs.getString("bairro"));
				bean.setCidade(rs.getString("cidade"));
				bean.setEstado(rs.getString("estado"));
				bean.setCep(rs.getString("cep"));
				bean.setCnpj(rs.getString("cnpj"));
				bean.setInscricaoEstadual(rs.getString("inscricao_estadual"));
				bean.setTelefone(rs.getString("telefone"));
				bean.setEmail(rs.getString("email"));
				bean.setPagamentoComissao(PagamentoComissao.valueOf(rs.getString("pagamento_comissao")));
				bean.setComissaoDireto(rs.getString("comissao_direto"));
				bean.setComissaoPreposto(rs.getString("comissao_preposto"));
				bean.setIdFabrica(rs.getString("id_fabrica"));

				fabricas.add(bean);
			}
			rs.close();
			stmt.close();
			return (List<T>) fabricas;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
	}
}
